﻿Imports System.Drawing

Public Class Slider
    Private sp As Point
    Private Min As Integer = 0
    Private Max As Integer = 100
    Private Dscale As Double
    Private Range As Integer
    Private M_Value As Integer
    Private KnobValue As Integer
    Public Event ValueChanged(ByVal NewVal As Integer)
    Private _isFocused As Boolean
    Private M_KnobWidth As Integer = 20
    Private M_KnobHeight As Integer = 20

    Private M_Right As Integer = 0
    Private M_Left As Integer = 0
    Private M_Center As Integer = 0
    Public Property KnobWidth() As Integer
        Get
            Return M_KnobWidth
        End Get
        Set(ByVal value As Integer)
            M_KnobWidth = value
            Refresh()
        End Set
    End Property
    Public Property KnobHeight() As Integer
        Get
            Return M_KnobHeight
        End Get
        Set(ByVal value As Integer)
            M_KnobHeight = value
            Refresh()
        End Set
    End Property
    Private Sub Slider_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
        Me.Select()
        Me._isFocused = True
        Refresh()
    End Sub

    Private Sub Slider_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        If Me._isFocused Then
            If e.Delta > 0 Then
                Knob.Top = Knob.Top - 1
            Else
                Knob.Top = Knob.Top + 1
            End If
            If Knob.Top < 10 Then Knob.Top = 10
            If Knob.Bottom > Me.Height - 10 Then Knob.Top = Me.Height - Knob.Height - 10
            KnobValue = ((Me.Height - 30) - (Knob.Top - 10))
            M_Value = KnobValue / Dscale
            RaiseEvent ValueChanged(M_Value)
        End If
    End Sub

    Private Sub Slider_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Range = Max - Min
        Dscale = (Me.Height - 30) / Range

        Dim G As Graphics = e.Graphics
        Dim Center As Integer = (Me.Width / 2) + M_Center
        Dim P As New Pen(Color.Black, 1)
        G.FillRectangle(Brushes.Blue, Center - 2, 10, 5, Me.Height - 20)

        Dim SL As Integer = Center - (M_KnobWidth / 2) - 10
        Dim SR As Integer = Center + (M_KnobWidth / 2) + 10

        For C = 0 To Range Step (Range / 10)
            Dim d As Integer = 15 + (C * Dscale)

            'G.DrawLine(Pens.Black, SL, d, SR, d)
        Next


        Knob.Left = Center - (M_KnobWidth / 2)
        Knob.Width = M_KnobWidth

    End Sub
    Private Sub Slider_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Knob.Width = (Me.Width - M_Right) - M_Left
        Knob.Height = 20
        Knob.Left = M_Left
        Knob.Top = 10
    End Sub

    Private Sub Knob_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Knob.MouseDown
        sp = Windows.Forms.Control.MousePosition
    End Sub

    Private Sub Knob_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Knob.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim P As Drawing.Point
            P = Windows.Forms.Control.MousePosition
            Dim Y As Integer
            Y = P.Y - sp.Y
            Knob.Top = Knob.Top + Y
            If Knob.Top < 10 Then Knob.Top = 10
            If Knob.Bottom > Me.Height - 10 Then Knob.Top = Me.Height - Knob.Height - 10
            sp = P
            KnobValue = ((Me.Height - 30) - (Knob.Top - 20))
            M_Value = KnobValue / Dscale
            RaiseEvent ValueChanged(M_Value)
        End If
    End Sub

    Private Sub Slider_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        Me._isFocused = False
        Refresh()
    End Sub
    Public Property RightOffset() As Integer
        Get
            Return M_Right
        End Get
        Set(ByVal value As Integer)
            M_Right = value
            Me.Refresh()
        End Set
    End Property
    Public Property LeftOffset() As Integer
        Get
            Return M_Left
        End Get
        Set(ByVal value As Integer)
            M_Left = value
            Me.Refresh()
        End Set
    End Property
    Public Property CenterOffset() As Integer
        Get
            Return M_Center
        End Get
        Set(ByVal value As Integer)
            M_Center = value
            Me.Refresh()
        End Set
    End Property
End Class
